package com.younion.action.manage;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.younion.common.base.BaseAction;
import com.younion.common.paginator.Paginator;

import cpcns.jhsks.util.MapCollectionUtils;

import com.younion.business.IScoreRuleService;
import com.younion.vo.ScoreRuleVO;

/**
 * @author Joe Chen
*/
public class ScoreRuleAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	protected static final Log logger = LogFactory.getLog(ScoreRuleAction.class);

	private IScoreRuleService scoreRuleService = null;

	private ScoreRuleVO scoreRuleVO = new ScoreRuleVO();

	private List<ScoreRuleVO> scoreRuleList;// 结果集列表
	/**
	 * 列表：翻页区；数据区
	 */
	@SuppressWarnings("unchecked")
	public String listScoreRule() {
		try {
			Map paramMap = null;
			if (scoreRuleVO != null) {
				paramMap = MapCollectionUtils.removeEmptyEntry(PropertyUtils.describe(scoreRuleVO));
			}
			// 获取总记录数
			int total = scoreRuleService.getCountByMap(paramMap);
			// 根据总记录数获取分页对象
			Paginator paginator = this.getPaginator(total);
			scoreRuleList = scoreRuleService.get(paramMap, paginator);
			// 保存分页对象
			this.savaPaginator(paginator);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return SUCCESS;
	}

	/**
	 * 新增
	 */
	public String addScoreRule() {
		return SUCCESS;
	}

	/**
	 * 修改保存，调度Jsp
	 */
	public String editScoreRule() {
		try {
			scoreRuleVO = scoreRuleService.getById(scoreRuleVO.getId().toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 保存动作
	 */
	public String saveScoreRule() {
		try {
			if(scoreRuleVO.getBottomLine() == null  
				||	(scoreRuleVO.getTopLine() != null && scoreRuleVO.getBottomLine() !=null 
							&& scoreRuleVO.getTopLine() <= scoreRuleVO.getBottomLine())
				){
				tipMessage = "区间上限必须大于区间下限！";
				return ERROR;
			}
			scoreRuleVO = scoreRuleService.save(scoreRuleVO);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 删除
	 */
	public String deleteScoreRule() {
		try {
			String pkId = request.getParameter("key");
			scoreRuleService.remove(Arrays.asList(pkId.split(",")));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查看鿴
	 */
	public String viewScoreRule() {
		try {
			String pkId = request.getParameter("key"); 
			scoreRuleVO = scoreRuleService.getById(pkId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			e.printStackTrace();
		}
		return SUCCESS;

	}

	public ScoreRuleVO getScoreRuleVO() {
		return scoreRuleVO;
	}

	public void setScoreRuleVO(ScoreRuleVO scoreRuleVO) {
		this.scoreRuleVO = scoreRuleVO;
	}

	public void setScoreRuleService(IScoreRuleService scoreRuleService) {
		this.scoreRuleService = scoreRuleService;
	}

	public List<ScoreRuleVO> getScoreRuleList() {
		return scoreRuleList;
	}

	public void setScoreRuleList(List<ScoreRuleVO> scoreRuleList) {
		this.scoreRuleList = scoreRuleList;
	}

}